﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Entity;
using System.Web;
using System.Web.Mvc;
using HdipCricketClub.Models;
using HdipCricketClub.ViewModels;

namespace HdipCricketClub.ViewModels
{
    public class CompletedScorecardViewModel
    {
        // properties

        public Fixture ScorecardFixture { get; set; }

        public ScorecardHeader ScorecardHeaderDetail { get; set; }

        public List<ClubFixturePlayer> ClubBatsmen { get; set; }

        public List<ClubFixturePlayer> ClubBowlers { get; set; }

        public ClubOtherFixtureDetail ClubOtherDetail { get; set; }

        public double ClubTotalBattingRuns { get; set; }

        public double ClubTotalExtras { get; set; }

        public double ClubTotalScore { get; set; }

        public List<OpponentFixturePlayer> OpponentBatsmen { get; set; }

        public List<OpponentFixturePlayer> OpponentBowlers { get; set; }

        public OpponentOtherFixtureDetail OpponentOtherDetail { get; set; }

        public double OpponentTotalBattingRuns { get; set; }

        public double OpponentTotalExtras { get; set; }

        public double OpponentTotalScore { get; set; }

        // database context

        CricketClubEntities db = new CricketClubEntities();

        // repositories

        FixtureRepository fixtureRepository = new FixtureRepository();
        ClubFixturePlayerRepository clubFixturePlayerRepository = new ClubFixturePlayerRepository();
        ClubOtherFixtureDetailRepository clubOtherFixtureDetailRepository = new ClubOtherFixtureDetailRepository();
        OpponentFixturePlayerRepository opponentFixturePlayerRepository = new OpponentFixturePlayerRepository();
        OpponentOtherFixtureDetailRepository opponentOtherFixtureDetailRepository = new OpponentOtherFixtureDetailRepository();
        ScorecardHeaderRepository scorecardHeaderRepository = new ScorecardHeaderRepository();

        // methods

        public CompletedScorecardViewModel ParseFormCollectionBattedFirst(FormCollection formCollection)
        {
            string[] scorecardFixtureId = formCollection.GetValues("ScorecardFixture.fixture_id");

            string[] resultId = formCollection.GetValues("ScorecardHeaderDetail.result_id");
            string[] wonToss = formCollection.GetValues("ScorecardHeaderDetail.won_toss");
            string[] battedFirst = formCollection.GetValues("ScorecardHeaderDetail.batted_first");

            // club batsmen
            string[] clubPlayerFixtureId = formCollection.GetValues("item.fixture_id");
            string[] clubPlayerPlayerId = formCollection.GetValues("item.player_id");
            string[] clubPlayerBatsmanNo = formCollection.GetValues("item.batsman_number");
            string[] clubPlayerHowOut = formCollection.GetValues("dismissal_id");
            string[] clubPlayerFielderName = formCollection.GetValues("op_fielder_name");
            string[] clubPlayerBowlerName = formCollection.GetValues("op_bowler_name");
            string[] clubPlayerBattingRuns = formCollection.GetValues("item.batting_runs");

            // club other details
            string[] ClubByes = formCollection.GetValues("ClubOtherDetail.byes");
            string[] ClubLegByes = formCollection.GetValues("ClubOtherDetail.leg_byes");
            string[] ClubWides = formCollection.GetValues("ClubOtherDetail.wides");
            string[] ClubNoBalls = formCollection.GetValues("ClubOtherDetail.no_balls");

            // opponent bowlers
            string[] oppPlayerFixtureId = formCollection.GetValues("item.op_fixture_id");
            string[] oppPlayerBatsmanNo = formCollection.GetValues("item.op_batsman_number");
            string[] oppPlayerBowlerNo = formCollection.GetValues("item.op_bowler_number");
            string[] oppPlayerName = formCollection.GetValues("item.op_player_name");
            string[] oppPlayerOvers = formCollection.GetValues("item.op_overs");
            string[] oppPlayerMaidens = formCollection.GetValues("item.op_maidens");
            string[] oppPlayerBowlingRuns = formCollection.GetValues("item.op_bowling_runs");
            string[] oppPlayerWickets = formCollection.GetValues("item.op_wickets");

            // opponent batsmen
            string[] oppPlayerHowOut = formCollection.GetValues("op_dismissal_id");
            string[] oppPlayerFielderId = formCollection.GetValues("fielder_id");
            string[] oppPlayerBowlerId = formCollection.GetValues("bowler_id");
            string[] oppPlayerBattingRuns = formCollection.GetValues("item.op_batting_runs");

            // opponent other details

            string[] oppByes = formCollection.GetValues("OpponentOtherDetail.byes");
            string[] oppLegByes = formCollection.GetValues("OpponentOtherDetail.leg_byes");
            string[] oppWides = formCollection.GetValues("OpponentOtherDetail.wides");
            string[] oppNoBalls = formCollection.GetValues("OpponentOtherDetail.no_balls");

            // club bowlers

            string[] clubPlayerBowlerNo = formCollection.GetValues("item.bowler_number");
            string[] clubPlayerOvers = formCollection.GetValues("item.overs");
            string[] clubPlayerMaidens = formCollection.GetValues("item.maidens");
            string[] clubPlayerBowlingRuns = formCollection.GetValues("item.bowling_runs");
            string[] clubPlayerWickets = formCollection.GetValues("item.wickets");

            // get fixture

            Fixture thisFixture = fixtureRepository.GetFixture(Int32.Parse(scorecardFixtureId[0]));

            // save scorecard header details

            ScorecardHeader thisScorecardHeader = new ScorecardHeader()
            {
                fixture_id = thisFixture.fixture_id,
                result_id = Int32.Parse(resultId[0]),
                won_toss = bool.Parse(wonToss[0]),
                batted_first = bool.Parse(battedFirst[0])
            };

            db.ScorecardHeaders.AddObject(thisScorecardHeader);
            db.SaveChanges();

            //  save club batsmen details

            double totalClubBattingRuns = 0;

            for (int i = 0; i < thisFixture.no_of_players; i++)
            {
                int thisBatsmanFixtureId = Int32.Parse(clubPlayerFixtureId[i]);
                int thisBatsmanPlayerId = Int32.Parse(clubPlayerPlayerId[i]);

                ClubFixturePlayer thisClubBatsman = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == thisBatsmanFixtureId && cfp.player_id == thisBatsmanPlayerId).Single();

                if (clubPlayerBatsmanNo[i] != "")
                {
                    thisClubBatsman.batsman_number = Int32.Parse(clubPlayerBatsmanNo[i]);
                }
                if (clubPlayerHowOut[i] != "")
                {
                    thisClubBatsman.how_out = Int32.Parse(clubPlayerHowOut[i]);
                }
                if (clubPlayerFielderName[i] != "")
                {
                    thisClubBatsman.fielder_name = (clubPlayerFielderName[i]);
                }
                if (clubPlayerBowlerName[i] != "")
                {
                    thisClubBatsman.bowler_name = (clubPlayerBowlerName[i]);
                }
                if (clubPlayerBattingRuns[i] != "")
                {
                    thisClubBatsman.batting_runs = double.Parse(clubPlayerBattingRuns[i]);
                    totalClubBattingRuns += (double)thisClubBatsman.batting_runs;
                }
                db.ObjectStateManager.ChangeObjectState(thisClubBatsman, EntityState.Modified);
                db.SaveChanges();
            }

            // save opponent batsmen details

            double totalOpponentBattingRuns = 0;

            for (int i = 0; i < thisFixture.no_of_players; i++)
            {
                int thisOppBatsmanFixtureId = Int32.Parse(oppPlayerFixtureId[i + oppPlayerBowlerNo.Length]);
                int thisOppBatsmanBatsmanNo = Int32.Parse(oppPlayerBatsmanNo[i + oppPlayerBowlerNo.Length]);

                OpponentFixturePlayer thisOpponentBatsman = new OpponentFixturePlayer()
                                                            {
                                                                op_fixture_id = thisOppBatsmanFixtureId,
                                                                op_batsman_number = thisOppBatsmanBatsmanNo
                                                            };
                if (oppPlayerName[i] != "")
                {
                    thisOpponentBatsman.op_player_name = oppPlayerName[i + oppPlayerBowlerNo.Length];
                }
                if (oppPlayerHowOut[i] != "")
                {
                    thisOpponentBatsman.op_how_out = Int32.Parse(oppPlayerHowOut[i]);
                }
                if (oppPlayerFielderId[i] != "")
                {
                    thisOpponentBatsman.op_fielder_name = Int32.Parse(oppPlayerFielderId[i]);
                }
                if (oppPlayerBowlerId[i] != "")
                {
                    thisOpponentBatsman.op_bowler_name = Int32.Parse(oppPlayerBowlerId[i]);
                }
                if (oppPlayerBattingRuns[i] != "")
                {
                    thisOpponentBatsman.op_batting_runs = double.Parse(oppPlayerBattingRuns[i]);
                    totalOpponentBattingRuns += (double)thisOpponentBatsman.op_batting_runs;
                }
                db.OpponentFixturePlayers.AddObject(thisOpponentBatsman);
                db.SaveChanges();
            }

            // save club bowler details

            for (int i = 0; i < clubPlayerBowlerNo.Length; i++)
            {
                int thisBowlerFixtureId = Int32.Parse(clubPlayerFixtureId[i + thisFixture.no_of_players]);
                int thisBowlerPlayerId = Int32.Parse(clubPlayerPlayerId[i + thisFixture.no_of_players]);

                ClubFixturePlayer thisClubBowler = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == thisBowlerFixtureId && cfp.player_id == thisBowlerPlayerId).Single();

                if (clubPlayerBowlerNo[i] != "")
                {
                    thisClubBowler.bowler_number = Int32.Parse(clubPlayerBowlerNo[i]);
                }
                if (clubPlayerOvers[i] != "")
                {
                    thisClubBowler.overs = double.Parse(clubPlayerOvers[i]);
                }
                if (clubPlayerMaidens[i] != "")
                {
                    thisClubBowler.maidens = double.Parse(clubPlayerMaidens[i]);
                }
                if (clubPlayerBowlingRuns[i] != "")
                {
                    thisClubBowler.bowling_runs = double.Parse(clubPlayerBowlingRuns[i]);
                }
                if (clubPlayerWickets[i] != "")
                {
                    thisClubBowler.wickets = double.Parse(clubPlayerWickets[i]);
                }
                db.ObjectStateManager.ChangeObjectState(thisClubBowler, EntityState.Modified);
                db.SaveChanges();
            }

            // save opponent bowler details

            for (int i = 0; i < oppPlayerBowlerNo.Length; i++)
            {
                int thisOppBowlerFixtureId = Int32.Parse(oppPlayerFixtureId[i]);
                int thisOppBowlerBatsmanNo = Int32.Parse(oppPlayerBatsmanNo[i]);

                OpponentFixturePlayer thisOpponentBowler = db.OpponentFixturePlayers.Where(ofp => ofp.op_fixture_id == thisOppBowlerFixtureId && ofp.op_batsman_number == thisOppBowlerBatsmanNo).Single();

                if (oppPlayerBowlerNo[i] != "")
                {
                    thisOpponentBowler.op_bowler_number = Int32.Parse(oppPlayerBowlerNo[i]);
                }
                if (oppPlayerOvers[i] != "")
                {
                    thisOpponentBowler.op_overs = double.Parse(oppPlayerOvers[i]);
                }
                if (oppPlayerMaidens[i] != "")
                {
                    thisOpponentBowler.op_maidens = double.Parse(oppPlayerMaidens[i]);
                }
                if (oppPlayerBowlingRuns[i] != "")
                {
                    thisOpponentBowler.op_bowling_runs = double.Parse(oppPlayerBowlingRuns[i]);
                }
                if (oppPlayerWickets[i] != "")
                {
                    thisOpponentBowler.op_wickets = Int32.Parse(oppPlayerWickets[i]);
                }

                db.ObjectStateManager.ChangeObjectState(thisOpponentBowler, EntityState.Modified);
                db.SaveChanges();
            }

            // save club other details

            ClubOtherFixtureDetail clubOtherFixtureDetail = new ClubOtherFixtureDetail() { fixture_id = thisFixture.fixture_id };
 
            if (ClubByes[0] != null)
            {
                clubOtherFixtureDetail.byes = double.Parse(ClubByes[0]);
            }
            if (ClubLegByes[0] != null)
            {
                clubOtherFixtureDetail.leg_byes = double.Parse(ClubLegByes[0]);
            }
            if (ClubWides[0] != null)
            {
                clubOtherFixtureDetail.wides = double.Parse(ClubWides[0]);
            }
            if (ClubNoBalls[0] != null)
            {
                clubOtherFixtureDetail.no_balls = double.Parse(ClubNoBalls[0]);
            }

            db.ClubOtherFixtureDetails.AddObject(clubOtherFixtureDetail);
            db.SaveChanges();
            
            double totalClubExtras = (double)(clubOtherFixtureDetail.byes + clubOtherFixtureDetail.leg_byes + clubOtherFixtureDetail.wides
                                   + clubOtherFixtureDetail.no_balls);

            double totalClubScore = totalClubBattingRuns + totalClubExtras;

            // save other opponent details

            OpponentOtherFixtureDetail opponentOtherFixtureDetail = new OpponentOtherFixtureDetail() { fixture_id = thisFixture.fixture_id }; 

            if (oppByes[0] != null)
            {
                opponentOtherFixtureDetail.byes = double.Parse(oppByes[0]);
            }
            if (oppLegByes[0] != null)
            {
                opponentOtherFixtureDetail.leg_byes = double.Parse(oppLegByes[0]);
            }
            if (oppWides[0] != null)
            {
                opponentOtherFixtureDetail.wides = double.Parse(oppWides[0]);
            }
            if (oppNoBalls[0] != null)
            {
                opponentOtherFixtureDetail.no_balls = double.Parse(oppNoBalls[0]);
            }

            db.OpponentOtherFixtureDetails.AddObject(opponentOtherFixtureDetail);
            db.SaveChanges();
            
            double totalOpponentExtras = (double)(opponentOtherFixtureDetail.byes + opponentOtherFixtureDetail.leg_byes + opponentOtherFixtureDetail.wides
                                   + opponentOtherFixtureDetail.no_balls);

            double totalOpponentScore = totalOpponentBattingRuns + totalOpponentExtras;
            
            // Set scorecard entered to true

            thisScorecardHeader.scorecard_entered = true;
            //db.ScorecardHeaders.Attach(thisScorecardHeader);
            db.ObjectStateManager.ChangeObjectState(thisScorecardHeader, EntityState.Modified);
            db.SaveChanges();
            
            // generate view model

            CompletedScorecardViewModel scorecardEntered = new CompletedScorecardViewModel();

            scorecardEntered.ScorecardFixture = thisFixture;
            scorecardEntered.ScorecardHeaderDetail = thisScorecardHeader;
            scorecardEntered.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(thisFixture);
            scorecardEntered.ClubTotalBattingRuns = totalClubBattingRuns;
            scorecardEntered.ClubOtherDetail = clubOtherFixtureDetail;
            scorecardEntered.ClubTotalExtras = totalClubExtras;
            scorecardEntered.ClubTotalScore = totalClubScore;
            scorecardEntered.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(thisFixture);
            scorecardEntered.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(thisFixture);
            scorecardEntered.OpponentTotalBattingRuns = totalOpponentBattingRuns;
            scorecardEntered.OpponentOtherDetail = opponentOtherFixtureDetail;
            scorecardEntered.OpponentTotalExtras = totalOpponentExtras;
            scorecardEntered.OpponentTotalScore = totalOpponentScore;
            scorecardEntered.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(thisFixture);

            // update player statistics

            PlayerSeasonStatistic playerSeasonStatistics = new PlayerSeasonStatistic();
            playerSeasonStatistics.UpdatePlayersStatistics(scorecardEntered.ClubBatsmen);

            return scorecardEntered;
        }

        public CompletedScorecardViewModel ParseFormCollectionBattedSecond(FormCollection formCollection)
        {
            string[] scorecardFixtureId = formCollection.GetValues("ScorecardFixture.fixture_id");

            string[] resultId = formCollection.GetValues("ScorecardHeaderDetail.result_id");
            string[] wonToss = formCollection.GetValues("ScorecardHeaderDetail.won_toss");
            string[] battedFirst = formCollection.GetValues("ScorecardHeaderDetail.batted_first");

            // club batsmen
            string[] clubPlayerFixtureId = formCollection.GetValues("item.fixture_id");
            string[] clubPlayerPlayerId = formCollection.GetValues("item.player_id");
            string[] clubPlayerBatsmanNo = formCollection.GetValues("item.batsman_number");
            string[] clubPlayerHowOut = formCollection.GetValues("dismissal_id");
            string[] clubPlayerFielderName = formCollection.GetValues("op_fielder_name");
            string[] clubPlayerBowlerName = formCollection.GetValues("op_bowler_name");
            string[] clubPlayerBattingRuns = formCollection.GetValues("item.batting_runs");

            // club other details
            string[] ClubByes = formCollection.GetValues("ClubOtherDetail.byes");
            string[] ClubLegByes = formCollection.GetValues("ClubOtherDetail.leg_byes");
            string[] ClubWides = formCollection.GetValues("ClubOtherDetail.wides");
            string[] ClubNoBalls = formCollection.GetValues("ClubOtherDetail.no_balls");

            // opponent bowlers
            string[] oppPlayerFixtureId = formCollection.GetValues("item.op_fixture_id");
            string[] oppPlayerBatsmanNo = formCollection.GetValues("item.op_batsman_number");
            string[] oppPlayerBowlerNo = formCollection.GetValues("item.op_bowler_number");
            string[] oppPlayerName = formCollection.GetValues("item.op_player_name");
            string[] oppPlayerOvers = formCollection.GetValues("item.op_overs");
            string[] oppPlayerMaidens = formCollection.GetValues("item.op_maidens");
            string[] oppPlayerBowlingRuns = formCollection.GetValues("item.op_bowling_runs");
            string[] oppPlayerWickets = formCollection.GetValues("item.op_wickets");

            // opponent batsmen
            string[] oppPlayerHowOut = formCollection.GetValues("op_dismissal_id");
            string[] oppPlayerFielderId = formCollection.GetValues("fielder_id");
            string[] oppPlayerBowlerId = formCollection.GetValues("bowler_id");
            string[] oppPlayerBattingRuns = formCollection.GetValues("item.op_batting_runs");

            // opponent other details

            string[] oppByes = formCollection.GetValues("OpponentOtherDetail.byes");
            string[] oppLegByes = formCollection.GetValues("OpponentOtherDetail.leg_byes");
            string[] oppWides = formCollection.GetValues("OpponentOtherDetail.wides");
            string[] oppNoBalls = formCollection.GetValues("OpponentOtherDetail.no_balls");

            // club bowlers

            string[] clubPlayerBowlerNo = formCollection.GetValues("item.bowler_number");
            string[] clubPlayerOvers = formCollection.GetValues("item.overs");
            string[] clubPlayerMaidens = formCollection.GetValues("item.maidens");
            string[] clubPlayerBowlingRuns = formCollection.GetValues("item.bowling_runs");
            string[] clubPlayerWickets = formCollection.GetValues("item.wickets");

            // get fixture

            Fixture thisFixture = fixtureRepository.GetFixture(Int32.Parse(scorecardFixtureId[0]));

            // save scorecard header details

            ScorecardHeader thisScorecardHeader = new ScorecardHeader()
            {
                fixture_id = thisFixture.fixture_id,
                result_id = Int32.Parse(resultId[0]),
                won_toss = bool.Parse(wonToss[0]),
                batted_first = bool.Parse(battedFirst[0])
            };

            db.ScorecardHeaders.AddObject(thisScorecardHeader);
            db.SaveChanges();

            //  save club batsmen details

            double totalClubBattingRuns = 0;

            for (int i = 0; i < thisFixture.no_of_players; i++)
            {
                int thisBatsmanFixtureId = Int32.Parse(clubPlayerFixtureId[i + clubPlayerBowlerNo.Length]);
                int thisBatsmanPlayerId = Int32.Parse(clubPlayerPlayerId[i + clubPlayerBowlerNo.Length]);

                ClubFixturePlayer thisClubBatsman = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == thisBatsmanFixtureId && cfp.player_id == thisBatsmanPlayerId).Single();

                if (clubPlayerBatsmanNo[i] != "")
                {
                    thisClubBatsman.batsman_number = Int32.Parse(clubPlayerBatsmanNo[i]);
                }
                if (clubPlayerHowOut[i] != "")
                {
                    thisClubBatsman.how_out = Int32.Parse(clubPlayerHowOut[i]);
                }
                if (clubPlayerFielderName[i] != "")
                {
                    thisClubBatsman.fielder_name = (clubPlayerFielderName[i]);
                }
                if (clubPlayerBowlerName[i] != "")
                {
                    thisClubBatsman.bowler_name = (clubPlayerBowlerName[i]);
                }
                if (clubPlayerBattingRuns[i] != "")
                {
                    thisClubBatsman.batting_runs = double.Parse(clubPlayerBattingRuns[i]);
                    totalClubBattingRuns += (double)thisClubBatsman.batting_runs;
                }
                db.ObjectStateManager.ChangeObjectState(thisClubBatsman, EntityState.Modified);
                db.SaveChanges();
            }

            // save opponent batsmen details

            double totalOpponentBattingRuns = 0;

            for (int i = 0; i < thisFixture.no_of_players; i++)
            {
                int thisOppBatsmanFixtureId = Int32.Parse(oppPlayerFixtureId[i]);
                int thisOppBatsmanBatsmanNo = Int32.Parse(oppPlayerBatsmanNo[i]);

                OpponentFixturePlayer thisOpponentBatsman = new OpponentFixturePlayer()
                {
                    op_fixture_id = thisOppBatsmanFixtureId,
                    op_batsman_number = thisOppBatsmanBatsmanNo
                };
                if (oppPlayerName[i] != "")
                {
                    thisOpponentBatsman.op_player_name = oppPlayerName[i];
                }
                if (oppPlayerHowOut[i] != "")
                {
                    thisOpponentBatsman.op_how_out = Int32.Parse(oppPlayerHowOut[i]);
                }
                if (oppPlayerFielderId[i] != "")
                {
                    thisOpponentBatsman.op_fielder_name = Int32.Parse(oppPlayerFielderId[i]);
                }
                if (oppPlayerBowlerId[i] != "")
                {
                    thisOpponentBatsman.op_bowler_name = Int32.Parse(oppPlayerBowlerId[i]);
                }
                if (oppPlayerBattingRuns[i] != "")
                {
                    thisOpponentBatsman.op_batting_runs = double.Parse(oppPlayerBattingRuns[i]);
                    totalOpponentBattingRuns += (double)thisOpponentBatsman.op_batting_runs;
                }
                db.OpponentFixturePlayers.AddObject(thisOpponentBatsman);
                db.SaveChanges();
            }

            // save club bowler details

            for (int i = 0; i < clubPlayerBowlerNo.Length; i++)
            {
                int thisBowlerFixtureId = Int32.Parse(clubPlayerFixtureId[i]);
                int thisBowlerPlayerId = Int32.Parse(clubPlayerPlayerId[i]);

                ClubFixturePlayer thisClubBowler = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == thisBowlerFixtureId && cfp.player_id == thisBowlerPlayerId).Single();

                if (clubPlayerBowlerNo[i] != "")
                {
                    thisClubBowler.bowler_number = Int32.Parse(clubPlayerBowlerNo[i]);
                }
                if (clubPlayerOvers[i] != "")
                {
                    thisClubBowler.overs = double.Parse(clubPlayerOvers[i]);
                }
                if (clubPlayerMaidens[i] != "")
                {
                    thisClubBowler.maidens = double.Parse(clubPlayerMaidens[i]);
                }
                if (clubPlayerBowlingRuns[i] != "")
                {
                    thisClubBowler.bowling_runs = double.Parse(clubPlayerBowlingRuns[i]);
                }
                if (clubPlayerWickets[i] != "")
                {
                    thisClubBowler.wickets = double.Parse(clubPlayerWickets[i]);
                }
                db.ObjectStateManager.ChangeObjectState(thisClubBowler, EntityState.Modified);
                db.SaveChanges();
            }

            // save opponent bowler details

            for (int i = 0; i < oppPlayerBowlerNo.Length; i++)
            {
                int thisOppBowlerFixtureId = Int32.Parse(oppPlayerFixtureId[i + oppPlayerBowlerNo.Length]);
                int thisOppBowlerBatsmanNo = Int32.Parse(oppPlayerBatsmanNo[i + oppPlayerBowlerNo.Length]);

                OpponentFixturePlayer thisOpponentBowler = db.OpponentFixturePlayers.Where(ofp => ofp.op_fixture_id == thisOppBowlerFixtureId && ofp.op_batsman_number == thisOppBowlerBatsmanNo).Single();

                if (oppPlayerBowlerNo[i] != "")
                {
                    thisOpponentBowler.op_bowler_number = Int32.Parse(oppPlayerBowlerNo[i]);
                }
                if (oppPlayerOvers[i] != "")
                {
                    thisOpponentBowler.op_overs = double.Parse(oppPlayerOvers[i]);
                }
                if (oppPlayerMaidens[i] != "")
                {
                    thisOpponentBowler.op_maidens = double.Parse(oppPlayerMaidens[i]);
                }
                if (oppPlayerBowlingRuns[i] != "")
                {
                    thisOpponentBowler.op_bowling_runs = double.Parse(oppPlayerBowlingRuns[i]);
                }
                if (oppPlayerWickets[i] != "")
                {
                    thisOpponentBowler.op_wickets = Int32.Parse(oppPlayerWickets[i]);
                }

                db.ObjectStateManager.ChangeObjectState(thisOpponentBowler, EntityState.Modified);
                db.SaveChanges();
            }

            // save club other details

            ClubOtherFixtureDetail clubOtherFixtureDetail = new ClubOtherFixtureDetail() { fixture_id = thisFixture.fixture_id };

            if (ClubByes[0] != null)
            {
                clubOtherFixtureDetail.byes = double.Parse(ClubByes[0]);
            }
            if (ClubLegByes[0] != null)
            {
                clubOtherFixtureDetail.leg_byes = double.Parse(ClubLegByes[0]);
            }
            if (ClubWides[0] != null)
            {
                clubOtherFixtureDetail.wides = double.Parse(ClubWides[0]);
            }
            if (ClubNoBalls[0] != null)
            {
                clubOtherFixtureDetail.no_balls = double.Parse(ClubNoBalls[0]);
            }

            db.ClubOtherFixtureDetails.AddObject(clubOtherFixtureDetail);
            db.SaveChanges();

            double totalClubExtras = (double)(clubOtherFixtureDetail.byes + clubOtherFixtureDetail.leg_byes + clubOtherFixtureDetail.wides
                                   + clubOtherFixtureDetail.no_balls);

            double totalClubScore = totalClubBattingRuns + totalClubExtras;

            // save other opponent details

            OpponentOtherFixtureDetail opponentOtherFixtureDetail = new OpponentOtherFixtureDetail() { fixture_id = thisFixture.fixture_id };

            if (oppByes[0] != null)
            {
                opponentOtherFixtureDetail.byes = double.Parse(oppByes[0]);
            }
            if (oppLegByes[0] != null)
            {
                opponentOtherFixtureDetail.leg_byes = double.Parse(oppLegByes[0]);
            }
            if (oppWides[0] != null)
            {
                opponentOtherFixtureDetail.wides = double.Parse(oppWides[0]);
            }
            if (oppNoBalls[0] != null)
            {
                opponentOtherFixtureDetail.no_balls = double.Parse(oppNoBalls[0]);
            }

            db.OpponentOtherFixtureDetails.AddObject(opponentOtherFixtureDetail);
            db.SaveChanges();

            double totalOpponentExtras = (double)(opponentOtherFixtureDetail.byes + opponentOtherFixtureDetail.leg_byes + opponentOtherFixtureDetail.wides
                                   + opponentOtherFixtureDetail.no_balls);

            double totalOpponentScore = totalOpponentBattingRuns + totalOpponentExtras;

            // Set scorecard entered to true

            thisScorecardHeader.scorecard_entered = true;
            //db.ScorecardHeaders.Attach(thisScorecardHeader);
            db.ObjectStateManager.ChangeObjectState(thisScorecardHeader, EntityState.Modified);
            db.SaveChanges();


            CompletedScorecardViewModel scorecardEntered = new CompletedScorecardViewModel();

            scorecardEntered.ScorecardFixture = thisFixture;
            scorecardEntered.ScorecardHeaderDetail = thisScorecardHeader;
            scorecardEntered.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(thisFixture);
            scorecardEntered.ClubTotalBattingRuns = totalClubBattingRuns;
            scorecardEntered.ClubOtherDetail = clubOtherFixtureDetail;
            scorecardEntered.ClubTotalExtras = totalClubExtras;
            scorecardEntered.ClubTotalScore = totalClubScore;
            scorecardEntered.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(thisFixture);
            scorecardEntered.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(thisFixture);
            scorecardEntered.OpponentTotalBattingRuns = totalOpponentBattingRuns;
            scorecardEntered.OpponentOtherDetail = opponentOtherFixtureDetail;
            scorecardEntered.OpponentTotalExtras = totalOpponentExtras;
            scorecardEntered.OpponentTotalScore = totalOpponentScore;
            scorecardEntered.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(thisFixture);

            // update player statistics

            PlayerSeasonStatistic playerSeasonStatistics = new PlayerSeasonStatistic();
            playerSeasonStatistics.UpdatePlayersStatistics(scorecardEntered.ClubBatsmen);

            return scorecardEntered;
        }

        public CompletedScorecardViewModel GetViewModelData(int id)
        {
            CompletedScorecardViewModel scorecard = new CompletedScorecardViewModel();
            scorecard.ScorecardFixture = fixtureRepository.GetFixture(id);
            scorecard.ScorecardHeaderDetail = scorecardHeaderRepository.GetScorecardHeader(scorecard.ScorecardFixture);
            scorecard.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecard.ScorecardFixture);
            scorecard.ClubTotalBattingRuns = clubFixturePlayerRepository.GetTotalBattingRuns(scorecard.ClubBatsmen);
            scorecard.ClubOtherDetail = clubOtherFixtureDetailRepository.GetClubOtherFixtureDetail(scorecard.ScorecardFixture);
            scorecard.ClubTotalExtras = clubOtherFixtureDetailRepository.GetTotalClubExtras(scorecard.ClubOtherDetail);
            scorecard.ClubTotalScore = scorecard.ClubTotalBattingRuns + scorecard.ClubTotalExtras;
            scorecard.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(scorecard.ScorecardFixture);
            scorecard.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecard.ScorecardFixture);
            scorecard.OpponentTotalBattingRuns = opponentFixturePlayerRepository.GetTotalBattingRuns(scorecard.OpponentBatsmen);
            scorecard.OpponentOtherDetail = opponentOtherFixtureDetailRepository.GetOpponentOtherFixtureDetail(scorecard.ScorecardFixture);
            scorecard.OpponentTotalExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecard.OpponentOtherDetail);
            scorecard.OpponentTotalScore = scorecard.OpponentTotalBattingRuns + scorecard.OpponentTotalExtras;
            scorecard.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(scorecard.ScorecardFixture);
            
            return scorecard;
        }


    }
}